package com.wanxi.user.util;

import at.favre.lib.crypto.bcrypt.BCrypt;

public class PasswordUtils {
    // 生成哈希密码
    public static String hashPassword(String password) {
        int costFactor = 12; // 设置哈希计算的成本因子（默认为10，数值越大，计算速度越慢）
        BCrypt.Hasher hasher = BCrypt.withDefaults();
        return hasher.hashToString(costFactor, password.toCharArray());
    }

    // 验证密码
    public static boolean verifyPassword(String password, String hashedPassword) {
        BCrypt.Verifyer verifyer = BCrypt.verifyer();
        BCrypt.Result result = verifyer.verify(password.toCharArray(), hashedPassword);
        return result.verified;
    }

    public static void main(String[] args) {
        String plainPassword = "123456";

        // 生成哈希密码
        String hashedPassword = hashPassword(plainPassword);
        System.out.println("Hashed Password: " + hashedPassword);

        // 验证密码
        boolean isPasswordCorrect = verifyPassword(plainPassword, hashedPassword);
        System.out.println("Password Correct: " + isPasswordCorrect);
    }
}
